// pages/Conveinfo/typelist/typelist.js
var app = getApp();
var ikz = require('../../Common/tools.js');
var arrs=[];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    banner_info: '',//banner图信息
    page: 1,//页数
    limit: 6,//每页条数
    conven_info: '',//优选信息
    prodis: 'none',//底部提示是否显示
    banner_info: '',//banner图信息
  },
  // 返回上一页
  goback:function(){
    wx.navigateBack({
      title:'返回',
      url:'../index/index'
    })
  },
  //banner变化
  swiperChange: function (e) {
    var imgnum = e.detail.current + 1;
    this.setData({
      imgnum: imgnum,
    });
  },
  //时间转换
  diaplayTime(data) {
    var str = data;
    //将字符串转换成时间格式
    var timePublish = new Date(str);
    //现在时间
    var timeNow = new Date();
    //当天0点时间
    var timeStart = new Date(new Date(new Date().toLocaleDateString()).getTime());
    var minute = 1000 * 60;
    var hour = minute * 60;
    var day = hour * 24;
    var diffValue = timeNow - timePublish;
    var diffHour = diffValue / hour;
    var diffMinute = diffValue / minute;

    if (diffValue < 0) {
      wx.showToast({
        title: '时间错误！',
        duration: 2000
      });
      return false;
    } else {
      //判断是今天
      if (timePublish >= timeStart) {
        if (diffHour > 1) {
          var result = parseInt(diffHour) + "小时前";
        } else if (diffMinute > 1) {
          var result = parseInt(diffMinute) + "分钟前";
        } else {
          var result = "刚刚发表";
        }
      } else {//判断不是今天
        var result = str;
      }
    }
    return result;
  },
  //跳转到详情页
  toContent: function (e) {
    var cid = e.currentTarget.dataset.cid;
    var data = {
      cid: cid,
    }
    ikz.nav('../content/content', 'n', data);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    ikz.over(that);
    var tid = options.tid;
    that.setData({
      tid: tid,
    });
    var page = 1;
    var limit = that.data.limit;
    var tid = that.data.tid;
    var data = {
      page: page,
      limit: limit,
      tid: tid,
    }
    var path = 'Conven/typelist';
    ikz.request(path, data, false, (res) => {
      console.log(res);
      if (res.data.status == 'success') {
        if (res.data.conven_info.length) {
          for (var i = 0; i < res.data.conven_info.length; i++) {
            res.data.conven_info[i].conven_addtime = that.diaplayTime(res.data.conven_info[i].conven_addtime);
          }
        }
        that.setData({
          conven_info: res.data.conven_info,
          page: page,
        });
        ikz.hide(that);
      } else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        })
      }

    });
    

    var path = 'Conven/index';
    ikz.request(path, data, false, (res) => {
      console.log(res.data)
      if (res.data.conven_info.length) {
        for (var i = 0; i < res.data.conven_info.length; i++) {
          res.data.conven_info[i].conven_addtime = that.diaplayTime(res.data.conven_info[i].conven_addtime);
          if (res.data.conven_info[i].conven_top == 2) {
            arrs.push(res.data.conven_info)
          }
        }
      }
      if (res.data.status == 'success') {
        var all_browse = res.data.shop_info.self_all_browse;
        var all_release = res.data.all_release;

        that.setData({
          banner_info: res.data.banner_info,
        });
        ikz.hide(that);
      } else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        })
      }

    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


  // 置顶
  toTop: function () {
    wx.pageScrollTo({
      scrollTop: 0
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // wx.showLoading({
    //   title: '玩命加载中',
    // })  
    
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  // 上拉加载更多
  onReachBottom: function () {
    console.log(1231231212121212112122)
    wx.showLoading({
      title: '玩命加载中',
    })
    var that = this;
    var page = that.data.page + 1;
    var old_info = that.data.conven_info;
    var tid = that.data.tid;
    that.setData({
      page: page,
    })
    var limit = that.data.limit;
    var data = {
      page: page,
      limit: limit,
      tid: tid,
    }
    var path = 'Conven/typelist';
    ikz.request(path, data, false, (res) => {
      console.log(res);
      if (res.data.status == 'success') {
        var new_info = res.data.conven_info;
        if (new_info.length) {

          for (var i = 0; i < new_info.length; i++) {
            new_info[i].conven_addtime = that.diaplayTime(new_info[i].conven_addtime);
          }
          var all_info = old_info.concat(new_info);
          var prodis = 'none';


        } else {
          var all_info = old_info;
          var prodis = 'block';
        }
        that.setData({
          conven_info: all_info,
          prodis: prodis,
        })
        // 隐藏导航栏加载框  
        wx.hideNavigationBarLoading();
        // 停止下拉动作  
        wx.stopPullDownRefresh();
        wx.hideLoading()
      } else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        })
      }

    });

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  /**
  * 页面相关事件处理函数--监听用户下拉动作
  */
  onPullDownRefresh: function () {
    console.log('aaaaaaaaaaa');
    wx.showLoading({
      title: '刷新中..',
    })
    var that = this;
    var page = that.data.page + 1;
    var old_info = that.data.conven_info;
    var tid = that.data.tid;
    that.setData({
      page: page,
    })
    var limit = that.data.limit;
    var data = {
      page: page,
      limit: limit,
      tid: tid,
    }
    var path = 'Conven/typelist';
    ikz.request(path, data, false, (res) => {
      console.log(res);
      if (res.data.status == 'success') {
        var new_info = res.data.conven_info;
        if (new_info.length) {

          for (var i = 0; i < new_info.length; i++) {
            new_info[i].conven_addtime = that.diaplayTime(new_info[i].conven_addtime);
          }
          var all_info = old_info.concat(new_info);
          var prodis = 'none';


        } else {
          var all_info = old_info;
          var prodis = 'block';
        }
        that.setData({
          conven_info: all_info,
          prodis: prodis,
        })
        // 隐藏导航栏加载框  
        wx.hideNavigationBarLoading();
        // 停止下拉动作  
        wx.stopPullDownRefresh();
        wx.hideLoading()
      } else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        })
      }

    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
 

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})